<?php

/**
 * Description of ContentUserBorrowings
 *
 * @author Honza, Kevin
 */
class ContentUserBorrowings implements IMainBodyWithInteraction {

  private $error;

  public function action() {
	  //allows to extend books
    if (isset($_GET['a'])) {
      if ($_GET['a'] == 'extend') {
        $idVyp = addslashes($_GET['ida']);


        $extensionAllowed = true;

        //maximum: 3 extends
        $sql = 'select * from ' . TBL_BORROWINGS . ' where id_borrowing = ' . $idVyp;
        $borrowing = DB::sql($sql);
        if ($borrowing[0]['num_of_extensions'] >= 3) {
          $extensionAllowed = false;
          $this->error = '<p class="error">The borrowing can be extended just 3-times.</p>';
        }

        //The book is requested. Extending of borrowing is not allowed.
        $sql = 'select * from ' . TBL_BORROWINGS;
        $sql .= ' join ' . TBL_BOOK_PRINTS . ' using (id_book_print)';
        $sql .= ' join ' . TBL_REQUESTS . ' using (id_book)';
        $sql .= ' where id_book_print = ' . $borrowing[0]['id_book_print'] . ' and state=\'platný\'';
        $requests = DB::sql($sql);
        if ($requests != null) {
          $extensionAllowed = false;
          $this->error = '<p class="error">The book is requested. Extending of borrowing is not allowed.</p>';
        }

        if ($extensionAllowed) {
          $sql = 'update ' . TBL_BORROWINGS . " set borrowed_until = CURDATE() + INTERVAL 30 DAY, num_of_extensions = num_of_extensions + 1 where id_borrowing = " . $idVyp;
          DB::sql($sql);
        }
      }
    }
  }

  public function applyForm($formID = NULL) {
    
  }
//getContent function called automatically
  public function getContent() {
    $content = '';
    $content .= '<h2>My borrowings</h2>';

    $this->action();
    $content .= $this->getTable();
    
    return $content;
  }
//create a form for borrowing 
  public function getForm($formID = NULL, $error = NULL) {
    
  }

  public function getSubmitedForm() {
    
  }
// get table to show table details
  public function getTable($tableID = NULL) {
    $auth = Auth::getInstance();
    $identity = $auth->getIdentity();
    $userID = $identity['id'];
// join table from borrowing table with book prints, books, user id,  
    $cols = 'id_borrowing,title, ifnull(DATE_FORMAT(borrowed_until, \'%Y-%m-%d\'),\'Requested\') as borrow_until, num_of_extensions';
    $sql = 'select ' . $cols . ' from ' . TBL_BORROWINGS . '';
    $sql .= ' join ' . TBL_BOOK_PRINTS . ' using (id_book_print)';
    $sql .= ' join ' . TBL_BOOKS . ' using (id_book)';
    $sql .= ' where id_user = ' . $userID . ' and date_of_returning is null';
    $sql .= ' order by title';
// send query to DB
    $result = DB::sql($sql);

    
// retult is empty - no borrowing books
    if (empty($result)) {
      return '<p>No borrowings</p>';
    }
    
    $table = '<table>';
    $table .= '<tr>';
    $table .= '<th>Title</th><th>Borrowed until</th><th>Number of extensions</th><th>Extend</th>';
    $table .= '</tr>';
    foreach ($result as $row) {
      
      $idBor = $row['id_borrowing'];


      $table .= '<tr>';
      $table .= '<td>' . $row['title'] . '</td>';
      $table .= '<td>' . $row['borrow_until'] . '</td>';
      $table .= '<td>' . $row['num_of_extensions'] . '</td>';
      $table .= '<td>';
      if ($row['zapujceno_do'] == 'Requested') {
        $table .= '&nbsp;';
      } else {
        $table .= '<a href="index.php?where=user&subwhere=borrowings&a=extend&ida=' . $idBor . '">Extend</a>';
      }
      $table .= '</td>';
      $table .= '</tr>';
    }
    $table .= '';
    $table .= '</table>';

    $table = $this->error . $table;
    return $table;
  }

}

?>
